package com.ke.jun.bg.testpetpackmvvm.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.viewpager2.widget.ViewPager2
import com.ke.jun.bg.petpackmvvm.base.viewmodel.BaseViewModel
import com.ke.jun.bg.petpackmvvm.view.gone
import com.ke.jun.bg.petpackmvvm.view.visible
import com.ke.jun.bg.testpetpackmvvm.R
import com.ke.jun.bg.testpetpackmvvm.app.base.BaseActivity
import com.ke.jun.bg.testpetpackmvvm.app.util.CacheUtil
import com.ke.jun.bg.testpetpackmvvm.app.util.SettingUtil
import com.ke.jun.bg.testpetpackmvvm.app.weight.banner.WelcomBannerAdapter
import com.ke.jun.bg.testpetpackmvvm.app.weight.banner.WelcomBannerViewHolder
import com.ke.jun.bg.testpetpackmvvm.databinding.ActivityWelcomeBinding
import com.zhpan.bannerview.BannerViewPager
import kotlinx.android.synthetic.main.activity_welcome.*

class WelcomeActivity :BaseActivity<BaseViewModel,ActivityWelcomeBinding>() {
    private var resList= arrayOf("唱歌","跳舞"," r rap ")
    private lateinit var mViewPager:BannerViewPager<String, WelcomBannerViewHolder>
    override fun layoutId()= R.layout.activity_welcome

    override fun initView(savedInstanceState: Bundle?) {
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT!==0){
            finish()
            return
        }

        mBindimng.click=ProxyClick()
        mBindimng.welcomeBaseview.setBackgroundColor(SettingUtil.getColor(this))
        mViewPager=findViewById(R.id.banner_view)
        if (CacheUtil.isFirst()){
            //是第一次打开app 显示引导页
            mBindimng.welcomeImage.gone()
            mViewPager.apply {
                adapter=WelcomBannerAdapter()
                setLifecycleRegistry(lifecycle)
                registerOnPageChangeCallback(object :ViewPager2.OnPageChangeCallback(){
                    override fun onPageSelected(position: Int) {
                        super.onPageSelected(position)
                        if (position==resList.size-1){
                            mBindimng.welcomeJoin.visible()
                        }else{
                            mBindimng.welcomeJoin.gone()
                        }
                    }
                })

                create(resList.toList())
            }
        }else{

            //不是第一打开
            mBindimng.welcomeImage.visible()
            mViewPager.postDelayed({
                startActivity(Intent(this, MainActivity::class.java))
                finish()
                //带点渐变动画
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            },300)
        }

    }



    inner class ProxyClick {
        fun toMain() {
            CacheUtil.setFirst(false)
            startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
            finish()
            //带点渐变动画
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }
    }
}